package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApAssociateWords;
import com.heima.search.mapper.ApAssociateWordsMapper;
import com.heima.search.model.Trie;
import com.heima.search.service.ApAssociateWordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ApAssociateWordsServiceImpl extends ServiceImpl<ApAssociateWordsMapper, ApAssociateWords> implements ApAssociateWordsService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult search(UserSearchDto dto) {
        //检查参数
        if(dto==null || dto.getPageSize()>50){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //模糊查询数据
        Page page = new Page<>(0, dto.getPageSize());
        List<ApAssociateWords>list=list(Wrappers.<ApAssociateWords>lambdaQuery().eq(ApAssociateWords::getAssociateWords,dto.getSearchWords()));
        return ResponseResult.okResult(list);

    }

    @Override
    public ResponseResult searchV2(UserSearchDto dto) {
        //从缓存中获取数据
        String associate_list = redisTemplate.opsForValue().get("associate_list");
        List<ApAssociateWords>apAssociateWords=null;
        if(StringUtils.isNotEmpty(associate_list)){
            //缓存中存在，直接获取
             apAssociateWords = JSON.parseArray(associate_list, ApAssociateWords.class);
        }else {
            //缓存中不存在，从数据库获取数据，存储到redis
            apAssociateWords=list(Wrappers.<ApAssociateWords>lambdaQuery().eq(ApAssociateWords::getAssociateWords,dto.getSearchWords()));
            redisTemplate.opsForValue().set("associate_list",JSON.toJSONString(apAssociateWords));
        }
        //构建trie数据结构。从trie中获取数据，封装返回
        Trie trie = new Trie();
        for (ApAssociateWords apAssociateWord : apAssociateWords) {
            trie.insert(apAssociateWord.getAssociateWords());
        }
        List<String> associateWords = trie.getData(dto.getSearchWords());
        if(apAssociateWords!=null && !apAssociateWords.isEmpty()){
            List<ApAssociateWords> wordsList = apAssociateWords.stream().map(x -> {
                ApAssociateWords words = new ApAssociateWords();
                words.setAssociateWords(String.valueOf(x));
                return words;
            }).collect(Collectors.toList());
            return ResponseResult.okResult(wordsList);
        }
        return ResponseResult.okResult();
    }
}
